Week 3 - Fitting a curve

In [5]:
# You may need this setup
using Plots
pyplot()
Out[5]:
Plots.PyPlotBackend()
In [6]:
using DelimitedFiles

Import the supplied data representing 15 pairs to x- and y-values.

In [7]:
data_tofit = readdlm("Week3_PR_Data.dat", '\t')
Out[7]:
15×2 Array{Float64,2}:
 0.1268     -1.6417  
 0.501309   -0.977698
 1.52801     0.527711
 1.70012     1.71152 
 1.99249     1.891   
 2.70608    -0.463428
 2.99493    -0.443567
 3.49185    -1.27518 
 3.50119    -0.6905  
 4.45992    -5.51613 
 4.93697    -6.0017  
 5.02329    -8.36417 
 5.04234    -7.92448 
 5.50739   -10.7748  
 5.56867   -10.9172  

Use for loop to print out all the values of data_tofit. The result should be numbers in 15 rows and two columns

In [8]:
# Write a ‘For’ loop to print out all the values of data_tofit

num_rows = length(data_tofit[:,1])

for i in 1:num_rows
    println(i, "\t", data_tofit[i,1], "\t", data_tofit[i,2])
end
1	0.1268004831284406	-1.6416953879765301
2	0.5013092807380928	-0.9776975375269383
3	1.5280121125586477	0.5277112203195138
4	1.7001225303407743	1.711524991194374
5	1.9924936253216172	1.8910000148140624
6	2.706075824201991	-0.46342779446395
7	2.9949319274309043	-0.4435666186385725
8	3.4918528112833935	-1.275179133203867
9	3.501191722475427	-0.6904995966451337
10	4.459924502120439	-5.51613079927097
11	4.936965850879389	-6.001703074115855
12	5.023289852369695	-8.364169009651015
13	5.042336980089736	-7.924477516763416
14	5.507392850419521	-10.774823709545498
15	5.568665171088307	-10.917187797703853

Assign data from data_tofit to variables x and y

In [9]:
# Create the arrays x and y, assigning x the first column of data_tofit and y the second column
x,y = data_tofit[:,1], data_tofit[:,2]
Out[9]:
([0.1268, 0.501309, 1.52801, 1.70012, 1.99249, 2.70608, 2.99493, 3.49185, 3.50119, 4.45992, 4.93697, 5.02329, 5.04234, 5.50739, 5.56867], [-1.6417, -0.977698, 0.527711, 1.71152, 1.891, -0.463428, -0.443567, -1.27518, -0.6905, -5.51613, -6.0017, -8.36417, -7.92448, -10.7748, -10.9172])

Then we do a scatterplot, this gives us the points the line must go through.

In [11]:
# Plot the x and y data points using a scatter plot of the x and y array variables
scatter(x,y)
Out[11]:

For the line, we need a function, which we now define. Note that the parameters a, b, c need not be passed to the function: we will keep resetting them to try to improve the fit.

In [17]:
# Create a function called parabfit, with x as the argument, returning a*x^2 + b*x + c
function parabfit(x)
    return a*x^2 + b*x + c
end
Out[17]:
parabfit (generic function with 1 method)

Let's check that we do get a reasonable parabola. Choose your own interval [xmin, xmax] and parameters a, b, c. If it looks too much like a straight line, chance your choices until it does.

In [14]:
# Create variables a, b and c, assigning each the value 1
a = 1
b = 1
c = 1

# Plot the function parabfit, for x values between -5 and 5 
x = range(-5,stop=5,step=1) 
y=parabfit.(x)
plot(x,y)

# could also do plot(parafit,-5,5)
Out[14]:

Now we choose a, b, c and plot the curve together with the points.

Note that by looking at where the data points lie, we can deduce some of the properties for a, b, c, as follows.

The plot must have a y-intersection that is close to 0, so c is close to 0. Also, the parabola is open downwards, so a must be negative. Finally, it has its maximum at a positive x, so b must be positive.

Use plot() to start with the scatter plot and plot!() to add the curve for parabfit. (There are other ways to do this ...)

In [15]:
# More plot!() tries.
data_tofit_x = data_tofit[:,1]
data_tofit_y = data_tofit[:,2]
scatter(data_tofit_x,data_tofit_y)

a = -0.75
b = 2.75
c = -1.75
# Plot the function parabfit, for x values between -5 and 5 
x = range(-5,stop=5,step=1) 
y=parabfit.(x)
plot!(x,y)
Out[15]:
In [18]:
# more plot!() tries.Another way to do this is 
data_tofit_x = data_tofit[:,1]
data_tofit_y = data_tofit[:,2]
scatter(data_tofit_x,data_tofit_y)

a = -0.75
b = 2.75
c = -1.75
# Plot the function parabfit, for x values between -5 and 5 
plot!(parabfit,0,6)
Out[18]: